package com.yitongmao.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.yitongmao.adapter.MyFragmentAdapter;
import com.yitongmao.fragment.AllFragment;
import com.yitongmao.fragment.FaHuoFragment;
import com.yitongmao.fragment.FukuanFragment;
import com.yitongmao.fragment.ShouHuoFragment;
import com.yitongmao.fragment.YiWanChengFragment;
import com.yitongmao.yitmfruit.R;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MyIndentActivity extends AppCompatActivity implements View.OnClickListener {

    @Bind(R.id.zhucehuitui)
    AutoLinearLayout zhucehuitui;
    private TextView tv_all, tv_fukuan, tv_fahuo, tv_shouhuo, tv_tuikuan;
    private View activity_all, activity_fukuan, activity_fahuo, activity_shouhuo, activity_tuikuan;
    private ViewPager mViewPager;
    private ArrayList fragments;
    private ImageView cursor_quanbu, cursor_fukuan, cursor_fahuo, cursor_shouhuo, cursor_tuikuan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        immerSion();
        setContentView(R.layout.activity_my_indent);
        ButterKnife.bind(this);
        initViewPager();
        initView();
        Intent intent0 = getIntent();
        Intent intent1 = getIntent();
        Intent intent2 = getIntent();
        Intent intent3 = getIntent();
        String num0 = intent0.getStringExtra("one");
        String num1 = intent1.getStringExtra("two");
        String num2 = intent2.getStringExtra("three");
        String num3 = intent3.getStringExtra("four");
        if (num0 != null) {
            if (num0.equals("0")) {
                mViewPager.setCurrentItem(1);
            }
        }
        if (num1 != null) {
            if (num1.equals("1")) {
                mViewPager.setCurrentItem(2);
            }
        }
        if (num2 != null) {
            if (num2.equals("2")) {
                mViewPager.setCurrentItem(3);
            }
        }
        if (num3 != null) {
            if (num3.equals("3")) {
                mViewPager.setCurrentItem(4);
            }
        }
        zhucehuitui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyIndentActivity.this.finish();
            }
        });
    }
    //沉浸式状态栏
    public void immerSion() {
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.BLACK);
        }
    }
    private void initView() {
        //TextView
        tv_all = (TextView) findViewById(R.id.tv_all);
        tv_fukuan = (TextView) findViewById(R.id.tv_fukuan);
        tv_fahuo = (TextView) findViewById(R.id.tv_fahuo);
        tv_shouhuo = (TextView) findViewById(R.id.tv_shouhuo);
        tv_tuikuan = (TextView) findViewById(R.id.tv_tuikuan);
        //View
        activity_all = (View) findViewById(R.id.activity_all);
        activity_fukuan = (View) findViewById(R.id.activity_fukuan);
        activity_fahuo = (View) findViewById(R.id.activity_fahuo);
        activity_shouhuo = (View) findViewById(R.id.activity_shouhuo);
        activity_tuikuan = (View) findViewById(R.id.activity_tuikuan);
        //横杠
        cursor_quanbu = (ImageView) findViewById(R.id.cursor_);
        cursor_fukuan = (ImageView) findViewById(R.id.cursor_fukuan);
        cursor_fahuo = (ImageView) findViewById(R.id.cursor_fahuo);
        cursor_shouhuo = (ImageView) findViewById(R.id.cursor_shouhuo);
        cursor_tuikuan = (ImageView) findViewById(R.id.cursor_tuikuan);
        //TextView的点击监听
        tv_all.setOnClickListener(this);
        tv_fukuan.setOnClickListener(this);
        tv_fahuo.setOnClickListener(this);
        tv_shouhuo.setOnClickListener(this);
        tv_tuikuan.setOnClickListener(this);
        tv_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.setCurrentItem(0);
            }
        });
        tv_fukuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.setCurrentItem(1);
            }
        });
        tv_fahuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.setCurrentItem(2);
            }
        });
        tv_shouhuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.setCurrentItem(3);
            }
        });
        tv_tuikuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewPager.setCurrentItem(4);
            }
        });
    }
    private void initViewPager() {
        mViewPager = (ViewPager) findViewById(R.id.myViewPager);
        fragments = new ArrayList<Fragment>();
        Fragment mAllFragment = new AllFragment();
        Fragment mfukuanFragment = new FukuanFragment();
        Fragment mFaHuoFragment = new FaHuoFragment();
        Fragment mShouHuoFragment = new ShouHuoFragment();
        Fragment mTuiKuanFragment = new YiWanChengFragment();
        fragments.add(mAllFragment);
        fragments.add(mfukuanFragment);
        fragments.add(mFaHuoFragment);
        fragments.add(mShouHuoFragment);
        fragments.add(mTuiKuanFragment);
        mViewPager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(), fragments));
        mViewPager.setCurrentItem(0);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i2) {
            }

            @Override
            public void onPageSelected(int i) {
                switch (i) {
                    case 0:
                        activity_all.setVisibility(View.VISIBLE);
                        activity_fukuan.setVisibility(View.INVISIBLE);
                        activity_fahuo.setVisibility(View.INVISIBLE);
                        activity_shouhuo.setVisibility(View.INVISIBLE);
                        activity_tuikuan.setVisibility(View.INVISIBLE);
                        //字体变色
                        tv_all.setTextColor(getResources().getColor(R.color.main_top_tab_color_2));
                        tv_fukuan.setTextColor(getResources().getColor(R.color.gray));
                        tv_fahuo.setTextColor(getResources().getColor(R.color.gray));
                        tv_shouhuo.setTextColor(getResources().getColor(R.color.gray));
                        tv_tuikuan.setTextColor(getResources().getColor(R.color.gray));
                        //横杠变色
                        cursor_quanbu.setImageResource(R.color.main_top_tab_color_2);
                        cursor_fukuan.setImageResource(R.color.white);
                        cursor_fahuo.setImageResource(R.color.white);
                        cursor_shouhuo.setImageResource(R.color.white);
                        cursor_tuikuan.setImageResource(R.color.white);
                        break;
                    case 1:
                        activity_all.setVisibility(View.INVISIBLE);
                        activity_fukuan.setVisibility(View.VISIBLE);
                        activity_fahuo.setVisibility(View.INVISIBLE);
                        activity_shouhuo.setVisibility(View.INVISIBLE);
                        activity_tuikuan.setVisibility(View.INVISIBLE);
                        //字体变色
                        tv_all.setTextColor(getResources().getColor(R.color.gray));
                        tv_fukuan.setTextColor(getResources().getColor(R.color.main_top_tab_color_2));
                        tv_fahuo.setTextColor(getResources().getColor(R.color.gray));
                        tv_shouhuo.setTextColor(getResources().getColor(R.color.gray));
                        tv_tuikuan.setTextColor(getResources().getColor(R.color.gray));
                        //横杠变色
                        cursor_quanbu.setImageResource(R.color.white);
                        cursor_fukuan.setImageResource(R.color.main_top_tab_color_2);
                        cursor_fahuo.setImageResource(R.color.white);
                        cursor_shouhuo.setImageResource(R.color.white);
                        cursor_tuikuan.setImageResource(R.color.white);
                        break;
                    case 2:
                        activity_all.setVisibility(View.INVISIBLE);
                        activity_fukuan.setVisibility(View.INVISIBLE);
                        activity_fahuo.setVisibility(View.VISIBLE);
                        activity_shouhuo.setVisibility(View.INVISIBLE);
                        activity_tuikuan.setVisibility(View.INVISIBLE);
                        //字体变色
                        tv_all.setTextColor(getResources().getColor(R.color.gray));
                        tv_fukuan.setTextColor(getResources().getColor(R.color.gray));
                        tv_fahuo.setTextColor(getResources().getColor(R.color.main_top_tab_color_2));
                        tv_shouhuo.setTextColor(getResources().getColor(R.color.gray));
                        tv_tuikuan.setTextColor(getResources().getColor(R.color.gray));
                        //横杠变色
                        cursor_quanbu.setImageResource(R.color.white);
                        cursor_fukuan.setImageResource(R.color.white);
                        cursor_fahuo.setImageResource(R.color.main_top_tab_color_2);
                        cursor_shouhuo.setImageResource(R.color.white);
                        cursor_tuikuan.setImageResource(R.color.white);
                        break;
                    case 3:
                        activity_all.setVisibility(View.INVISIBLE);
                        activity_fukuan.setVisibility(View.INVISIBLE);
                        activity_fahuo.setVisibility(View.INVISIBLE);
                        activity_shouhuo.setVisibility(View.VISIBLE);
                        activity_tuikuan.setVisibility(View.INVISIBLE);
                        //字体变色
                        tv_all.setTextColor(getResources().getColor(R.color.gray));
                        tv_fukuan.setTextColor(getResources().getColor(R.color.gray));
                        tv_fahuo.setTextColor(getResources().getColor(R.color.gray));
                        tv_shouhuo.setTextColor(getResources().getColor(R.color.main_top_tab_color_2));
                        tv_tuikuan.setTextColor(getResources().getColor(R.color.gray));
                        //横杠变色
                        cursor_quanbu.setImageResource(R.color.white);
                        cursor_fukuan.setImageResource(R.color.white);
                        cursor_fahuo.setImageResource(R.color.white);
                        cursor_shouhuo.setImageResource(R.color.main_top_tab_color_2);
                        cursor_tuikuan.setImageResource(R.color.white);
                        break;
                    case 4:
                        activity_all.setVisibility(View.INVISIBLE);
                        activity_fukuan.setVisibility(View.INVISIBLE);
                        activity_fahuo.setVisibility(View.INVISIBLE);
                        activity_shouhuo.setVisibility(View.INVISIBLE);
                        activity_tuikuan.setVisibility(View.VISIBLE);
                        //字体变色
                        tv_all.setTextColor(getResources().getColor(R.color.gray));
                        tv_fukuan.setTextColor(getResources().getColor(R.color.gray));
                        tv_fahuo.setTextColor(getResources().getColor(R.color.gray));
                        tv_shouhuo.setTextColor(getResources().getColor(R.color.gray));
                        tv_tuikuan.setTextColor(getResources().getColor(R.color.main_top_tab_color_2));
                        //横杠变色
                        cursor_quanbu.setImageResource(R.color.white);
                        cursor_fukuan.setImageResource(R.color.white);
                        cursor_fahuo.setImageResource(R.color.white);
                        cursor_shouhuo.setImageResource(R.color.white);
                        cursor_tuikuan.setImageResource(R.color.main_top_tab_color_2);
                    default:
                        return;
                }
            }
            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });
    }

    @Override
    public void onClick(View v) {

    }
}
